<template>
  <view>
    <view style="" @click="show = true">
      <span>{{serveTime}}</span>
    </view>
	<view class="" v-if="sum<=8">
		可预约人数加载中....请稍后点击
	</view>
    <u-select :defaultValue="defaultTime" v-model="show" mode="mutil-column-auto" :list="list"
             @confirm="timeConfirm" @cancel="retFalse">
      <template #loading>
        <view class="loading">加载中...</view>
      </template>
    </u-select>
  </view>
</template>

<script>
export default {
  props: {
    TJTestId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
		sum:0,
      defaultTime: [0, 0],
      show: false,
      serveTime: "请选择预约时间",
      list: [],
      isLoading: false // 新增 loading 状态
    };
  },
  async created() {
    this.isLoading = true; // 开始加载时设置为 true
    let date = new Date();
    let myTimeList = [];
    let weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    let myHour = date.getHours();
    let myMinus = date.getMinutes();
    let myYear = date.getFullYear();
    let myMonth = date.getMonth();
    let myDate = date.getDate();
    let nowTime = myHour * 60 + myMinus;
    let allTime = 24 * 60;
    for (let i = 0; i <= 10; i++) {
      let newDate = new Date(myYear, myMonth, myDate + i);
      let year = newDate.getFullYear();
      let month = newDate.getMonth() + 1;
      let date = newDate.getDate();
      let day = newDate.getDay();
      let labelMytime = `(${weekday[day]}) ${year}-${month}-${date}`;
      let valueMytime = `${year}-${month}-${date}`;
      // 等待 getTimeList 的结果
      let children = await this.getTimeList(valueMytime);
      myTimeList.push({
        value: `${valueMytime}`,
        label: labelMytime,
        children: children
      });
    }
    this.list = myTimeList;
    this.isLoading = false; // 加载完成后设置为 false
  },
  methods: {
    retFalse() {
      this.$emit("retFalse");
    },
    retTime(num) {
      if (num >= 10) {
        return num.toString();
      } else {
        return '0' + num.toString();
      }
    },
    timeConfirm(e) {
      let dateList = e[0].value.split(' ');
      let timeList = e[1].value.split(' ');
      this.defaultTime = [dateList[1], timeList[1]];
      this.serveTime = dateList[0];

      this.$emit("getTime", this.serveTime,e[1].value);
    },
    async getTimeList(valueMytime) {
      let itemList = [];
      // 等待 GetPerCount 的结果
      let sum = await this.GetPerCount(valueMytime);
	  if(sum<0)sum=0
	 
      itemList.push({
        value: sum.toString(),
        label: '当日可预约人数' + sum
      });
      return itemList;
    },
    async GetPerCount(valueMytime) {
      try {
        let res = await this.$request("TJDisTime", "GetPerCount", {
          TJTestId: this.TJTestId,
          TimeSpace: valueMytime
        });
        // console.log("可预约人数", res);
        if (res.code === 1) {
			this.sum+=1
          return res.data;
        } else {
          console.warn("请求失败，错误信息:", res.msg);
          return '获取失败';
        }
      } catch (error) {
        console.error("请求异常:", error);
        return '';
      }
    }
  }
};
</script>

<style>
.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  font-size: 16px;
  color: #999;
}
</style>